﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess.Classes;
using System.IO;

public partial class Admin_EditProduct : System.Web.UI.Page
{
    HangHoa data;
    ChiTietHangHoa ctdata;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            PopulateControls();
    }
    private void PopulateControls()
    {
        // Lay danh sach Category dua vao DropDownList (DDL)
        // Dat value cho DDL la truong CategoryID
        ddlCategory.DataValueField = "MaThuongHieu";
        // Dat phan hien thi (text) la truong Name
        ddlCategory.DataTextField = "TenThuongHieu";
        ddlCategory.DataSource = ThuongHieu.All();
        ddlCategory.DataBind();
        string cid = Request.QueryString["cid"] ?? "";
        // Neu co QueryString cid ==> cap nhat Product
        if (cid != "")
        {
            // Lay Product theo gia tri cid
            data = HangHoa.Single(cid);
            ctdata = ChiTietHangHoa.Single(cid);
            // Khong ton tai Product nay trong he thong 
            // chuyen huong ve trang Product.aspx
            if (data == null)
                Response.Redirect("~/Admin/Product.aspx");
            // Nguoc lai thi dua du lieu vao form de tien hanh cap nhap
            // Dat ten trang web
            lblTitle.Text = "Edit Product";
            // Luu lai gia tri ID cua Product hien dang cap nhat
            lblId.Text = data.MaHangHoa.ToString();
            // Gan cac thong tin con lai vao form
            txtMa.Text = data.MaHangHoa;
            txtMa.Enabled = false;
            txtName.Text = data.TenHangHoa;
            txtPrice.Text = data.GiaMua.ToString();
            //lblImage.Text = data.HinhAnh;
            txtImage.Text = data.HinhAnh;
            txtCPU.Text = ctdata.CPU;
            txtDisplay.Text = ctdata.Display;
            txtHDD.Text = ctdata.HDD;
            txtLan.Text = ctdata.Lan;
            txtPin.Text = ctdata.Pin;
            txtRam.Text = ctdata.Ram;
            txtTL.Text = ctdata.TrongLuong;
            txtVGA.Text = ctdata.VGA;
            txtWc.Text = ctdata.Webcam;
            txtWf.Text = ctdata.Wifi;

            chkPromoFront.Checked = data.TrangThai == 1 ? true : false;
            ddlCategory.SelectedValue = data.MaThuongHieu.ToString();
            btnReset.Visible = false;
        }
        else
        {
            // Khong co QueryString cid ==> them moi Product
            // Dat ten trang web
            lblTitle.Text = "Add Product";
            // Hien nut reset (xoa trang form de nhap lai)
            btnReset.Visible = true;
        }
    }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        // Them su kien cho nut Save
        btnSave.Click += new EventHandler(btnSave_Click);
    }
    // Lay du lieu tu form de them moi/cap nhat
    private void GetData()
    {
        data = null;
        ctdata = null;
        if (lblId.Text != "")
        {
            // lay thong tin cu tu Database de cap nhat
            data = HangHoa.Single(lblId.Text);
            ctdata = ChiTietHangHoa.Single(lblId.Text);
        }
        else
        {
            data = new HangHoa(); // them moi
            data.MaHangHoa = txtMa.Text;
            ctdata = new ChiTietHangHoa();
            ctdata.MaHangHoa = txtMa.Text;
        }
        data.TenHangHoa = txtName.Text;
        if (txtPrice.Text != "")
            data.GiaMua = Convert.ToInt32(txtPrice.Text);
        //data.HinhAnh = lblImage.Text;
        data.HinhAnh = txtImage.Text;
        data.MaThuongHieu = ddlCategory.SelectedValue;
        data.TrangThai = chkPromoFront.Checked ? 1 : 0;
        ctdata.CPU = txtCPU.Text;
        ctdata.Display = txtDisplay.Text;
        ctdata.HDD = txtHDD.Text;
        ctdata.Lan = txtLan.Text;
        ctdata.Pin = txtPin.Text;
        ctdata.Ram = txtRam.Text;
        ctdata.TrongLuong = txtTL.Text;
        ctdata.VGA = txtVGA.Text;
        ctdata.Webcam = txtWc.Text;
        ctdata.Wifi = txtWf.Text;
        //return data;
    }
    void btnSave_Click(object sender, EventArgs e)
    {
        // Neu tat ca du lieu deu duoc nhap hop le
        if (Page.IsValid)
        {
            bool rs = false;
            // Lay du lieu tu form
            //HangHoa data = GetData();
            GetData();
            // ID > 0 ==> Cap nhat va hien thong bao
            if (lblId.Text != "")
            {
                rs = HangHoa.Update(data) 
                    && ChiTietHangHoa.Update(ctdata);
                lblStatus.Text = rs ? "Update successful" : "Update failed";
            }
            else
            {
                rs = HangHoa.Add(data) 
                    && ChiTietHangHoa.Add(ctdata);
                lblStatus.Text = rs ? "Insert successful" : "Insert failed";
                // Neu them thanh cong thi xoa trang form de them tiep
                if (rs)
                    ResetForm();
                    
            }
        }
    }
    private void ResetForm()
    {
        txtName.Text = "";
        txtPrice.Text = "";
        //lblImage.Text = "";
        txtImage.Text = "";
        chkPromoFront.Checked = false;
        ddlCategory.SelectedValue = "0";
        txtCPU.Text = "";
        txtDisplay.Text = "";
        txtHDD.Text = "";
        txtLan.Text = "";
        txtMa.Text = "";
        txtPin.Text = "";
        txtRam.Text = "";
        txtTL.Text = "";
        txtVGA.Text = "";
        txtWc.Text = "";
        txtWf.Text = "";
    }

       // co the cau hinh các file hop le o web.config
// sau do them phuong thuc o GlobalConfiguration.cs de lay gia tri do 
    bool CheckFileType(string fileName)
    {
        string ext = Path.GetExtension(fileName);
        switch (ext.ToLower())
        {
            case ".gif":
                return true;
            case ".png":
                return true;
            case ".jpg":
                return true;
            case ".jpeg":
                return true;
            default:
                return false;
         }
    }
    // Ham upload chung dung cho ca 2 button upload
// fileupload: dang upload thumbnail hay image
// label: dung de hien thi ten hinh anh
    void Upload(FileUpload fileupload, Label label)
    {
        try
        {
            // Kiem tra co file upload va kich thuoc file <= 500KB
            if (fileupload.HasFile && 
            fileupload.PostedFile.ContentLength <= 500000)
            {
                // Kiem tra loai file
               if (CheckFileType(fileupload.FileName))
               {
                    // Lay duong dan tuong doi o server
                    String filePath = "~/ProductImages/" + fileupload.FileName;
                    // Luu vao duong dan o tren
                    // MapPath(filePath) ==> chuyen ve duong dan tuyet doi
                    fileupload.SaveAs(MapPath(filePath));
                    label.Text = fileupload.FileName;
                    lblStatus.Text = "Upload successful";
                }
            }
            else
            {
                lblStatus.Text = "Uploading image failed";
            }
        }
        catch (Exception) 
        {
            lblStatus.Text = "Uploading image failed";
        }
    }
    

    void btnUploadImage_Click(object sender, EventArgs e)
    {
        // MapPath(filePath)
        //Upload(fuImage, lblImage);
    }
}